package com.yuanfeng.userms.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.zxing.NotFoundException;
import com.google.zxing.WriterException;
import com.yuanfeng.commoms.annnotation.SysLog;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.base.MemberMessageListIntegralDTO;
import com.yuanfeng.commoms.dto.shop.GateStoreUpdateDTO;
import com.yuanfeng.commoms.dto.userms.UserPointLogDTO;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.job.AcquiescenceVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.dto.MemberMessageListDTO;
import com.yuanfeng.userms.vo.UserPersonalBaseVO;
import io.seata.core.exception.TransactionException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/**
 * @ClassName UserInfoController
 * @Description 用户基础信息
 * @Author hyq
 * @Date 2023/5/18 15:56
 * @Version 1.0
 **/
@RestController
@RequestMapping("user/ms")
public class UserInfoController extends BaseController{


    /**
     * 根据手机号查用户信息
     *
     * @param userMobile 手机号
     * @return 结果
     */
    @PostMapping("/infoByMobile")
    public UserInfoVO queryPhoneNumCount2(@RequestParam("userMobile") String userMobile) {
        UserInfoVO result = userInfoService.getUserInfoByMobile(userMobile);
        return result;
    }

    /**
     * 查询用户信息及其关联表
     *
     * @param vo
     * @return 结果
     */
    @PostMapping("/querUserInfoByParams")
    public ResponseResult querUserInfoByParams(@RequestBody UserInfoVO vo) {
        ResponseResult result = userInfoService.querUserInfoByParams(vo);
        return result;
    }


    /**
     * 查询用户信息
     *
     * @param userInfoVO
     * @return 结果
     */
    @PostMapping("/queryUserInfo")
    public ResponseResult queryUserInfo(@RequestBody UserInfoVO userInfoVO) {
        ResponseResult result = userInfoService.queryUserInfo(userInfoVO);
        return result;
    }

    /**
     * 查询用户信息单表
     *
     * @param userInfoVO
     * @return 结果
     */
    @PostMapping("/queryUserVO")
    public ResponseResult queryUserInfoByUserInfoVO(@RequestBody Map<String, String> userInfoVO) {
        ResponseResult result = userInfoService.queryUserInfoByUserInfoVO(userInfoVO);
        return result;
    }


    /**
     * 根据userId获取用户信息
     *
     * @param userId
     * @return 结果
     */
    @GetMapping("/queryUserInfoByUserId")
    public ResponseResult queryUserInfoByUserId(@RequestParam("userId") String userId) {
        ResponseResult result = userInfoService.queryUserInfoByUserId(userId);
        return result;
    }


    /**
     * 修改用户表信息
     *
     * @param map
     * @return
     */
    @PostMapping("/updateUserInfoByMap")
    public ResponseResult updateUserInfoByMap(@RequestBody Map<String, String> map) {
        ResponseResult result = userInfoService.updateUserInfoByMap(map);
        return result;
    }


    /**
     * 修改用户积分
     *
     * @param param
     * @return
     */
    @PostMapping("/updateMemberIntegral")
    public ResponseResult updateMemberIntegral(@RequestBody MemberMessageListIntegralDTO param) {
        ResponseResult result = userInfoService.updateMemberIntegral(param);
        return result;
    }


    /**
     * 用户注册
     *
     * @param map
     * @return
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody Map<String, String> map) throws IOException, TransactionException {
        ResponseResult result = userInfoService.register(map);
        return result;
    }

    /**
     * 更新用户新人状态 feign
     * @param paraMap
     */
    @PostMapping("/updateUser/isnew")
    public Integer updateIsNew(@RequestBody Map<String, String> paraMap){
        Integer result=userInfoService.updateIsNew(paraMap);
        return result;
    }

    /**
     * feign 根据用户id 查询当前用户有多少积分
     *
     * @param userId
     * @return
     */
    @PostMapping("/getMemberIntegralByUserId")
    public Integer getMemberIntegralByUserId(@RequestParam("userId") String userId) {
        Integer result=userInfoService.queryMemberMessageListIntegral(userId);
        return result;
    }

    /**
     * 查询用户总积分
     */
    @PostMapping("/queryPersonalDiscount")
    public ResponseResult queryPersonalDiscount(@RequestBody UserTokenBeanDTO userTokenBeanDTO) {
        ResponseResult result = userInfoService.queryPersonalDiscount(userTokenBeanDTO);
        return result;
    }

    /**
     * 查询用户积分信息-获取会员等级
     */
    @PostMapping("/getuserMemberInformation")
    public ResponseResult getUserMemberInformation(@RequestBody Map<String, String> paraMap){
        ResponseResult result = userInfoService.getUserMemberInformation(paraMap);
        return result;
    }
    /**
     * 校验用户是否已经注册
     * @param paraMap
     * @return
     */
    @PostMapping("/checkPhoneNumberExist")
    public String checkPhoneNumber(@RequestBody Map<String, String> paraMap){
        String result=userInfoService.checkPhoneNumber(paraMap);
        return result;
    }

    /**
     * 企业用户注册
     */
    @PostMapping("/business/register")
    public ResponseResult businessegInsert(@RequestBody Map<String, String> map) throws IOException, TransactionException {
        ResponseResult result = userInfoService.businessegInsert(map);
        return result;
    }
    /**
     * 更新用户信息
     */
    @PostMapping("/info/update")
    public ResponseResult updateUcenterInfo(@RequestBody Map<String, Object> paraMap){
        ResponseResult result = userInfoService.updateUcenterInfo(paraMap);
        return result;
    }
    /**
     * 转账确认转账方信息
     */
    @PostMapping("/checkPay/transferInfo")
    public ResponseResult checkPayTransferInfo(@RequestBody Map<Object, Object> map) {
        ResponseResult result = userInfoService.checkPayTransferInfo(map);
        return result;
    }
    /**
     * 用户退出登录
     */
    @PostMapping("/userLogOut")
    public ResponseResult userLogOut(@RequestBody Map<String, String> paraMap) {
        ResponseResult result = userInfoService.userLogOut(paraMap);
        return result;
    }


    /**
     * 根据token返回用户信息
     *
     * @return
     * @throws IOException
     */
    @PostMapping("/getuser")
    public ResponseResult getUser(@RequestBody Map<String, String> paraMap) throws IOException, IllegalAccessException {
        ResponseResult result = userInfoService.getUser(paraMap);
        return result;
    }

    /**
     * 更新用户头像
     *
     * @param paraMap
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @PostMapping("/uploadPhoto")
    @SysLog("更新用户头像")
    public ResponseResult uploadImg(@RequestBody Map<String, String> paraMap) {
        ResponseResult result = userInfoService.uploadImg(paraMap);
        return result;
    }


    /**
     * 转账确认转账方信息
     *
     * @param map
     * @return
     */
    @PostMapping("/checkPay/transferInfoByMap")
    public ResponseResult checkPayTransferInfoByMap(@RequestBody Map<Object, Object> map) {
        ResponseResult result = userInfoService.checkPayTransferInfoByMap(map);
        return result;
    }

    /**
     * 获取账户信息
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/userSetting/getInfo")
    public ResponseResult getUcenterSettingByUserId(@RequestBody Map<String, String> paraMap){
        ResponseResult result = userInfoService.getUcenterSettingByUserId(paraMap);
        return result;
    }
    /**
     * 根据shopId 查询店铺信息
     */
    @GetMapping("/queryShopUserInfo")
    public ResponseResult queryShopUserInfo(@RequestParam("shopId") Integer shopId){
        ResponseResult result = userInfoService.queryShopUserInfo(shopId);
        return result;
    }

    /**
     * 检测验证码是否正确和更换绑定手机
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/updateUserMobile")
    public ResponseResult updateUserMobile(@RequestBody Map<String, String> paraMap) {
        ResponseResult result = userInfoService.updateUserMobile(paraMap);
        return result;
    }


    /**
     * 根据token生成带有userId的二维码和注册页面链接
     * @param paraMap
     * @return
     */
    @PostMapping("/register/createqr/user")
    public ResponseResult qrUser(@RequestBody Map<String, String> paraMap) throws IOException, NotFoundException, WriterException {
        ResponseResult result = userInfoService.qrUser(paraMap);
        return result;

    }


    /**
     * 查询商家客户，根据shopid查询
     * @param paraMap
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     * @throws NotFoundException
     * @throws WriterException
     */
    @PostMapping("/share/myCustomer")
    public ResponseResult myCustomer(@RequestBody Map<String, Object> paraMap)
            throws JsonParseException, JsonMappingException, IOException, NotFoundException, WriterException {
        ResponseResult result = userInfoService.myCustomer(paraMap);
        return result;

    }

    /**
     * 查询个人信息
     * @param paraMap
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @PostMapping("/information/getUserInfo")
    public ResponseResult getUserInfo(@RequestBody Map<String, Object> paraMap)
            throws JsonParseException, JsonMappingException, IOException {
        ResponseResult result = userInfoService.getUserInfo(paraMap);
        return result;

    }

    /**
     * 修改个人信息
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/updateUserInfo")
    @SysLog("修改个人信息")
    public ResponseResult updateUserInfo(@RequestBody Map<String, String> paraMap) throws ParseException {
        ResponseResult result = userInfoService.updateUserInfo(paraMap);
        return result;
    }

    /**
     * 根据手机号查询用户是否存在
     *
     * @param map
     * @return
     */
    @PostMapping("/userAccountByMap")
    public ResponseResult getUserAccountByMap(@RequestBody Map<String, Object> map) {
        Integer type = userInfoService.checkRepeateUserAccount(map);

        return type == CommonStatusEnum.FAIL.getCode() ? ResponseResult.fail(type) : ResponseResult.success(type);
    }


    /**
     * 忘记密码
     *
     * @param map
     * @return
     */
    @PostMapping("/forgotPassWord")
    public ResponseResult forgotPassword(@RequestBody Map<String, Object> map) throws IOException {
        ResponseResult result = userInfoService.forgotPassword(map);
        return result;
    }


    /**
     * 添加修改用户积分记录 用与feign调用
     * @param param
     * @return
     */
    @PostMapping("/fegin/addPointsLog")
    public ResponseResult feginAddpointsLog(@RequestBody UserPointLogDTO param) {
        ResponseResult result = userInfoService.feginAddpointsLog(param);
        return result;
    }

    /**
     * 登录名动态校验
     * @param map
     * @return
     */
    @PostMapping("/checkPhone")
    public ResponseResult checkPhone(@RequestBody Map<String, Object> map){
        ResponseResult result = userInfoService.checkPhone(map);
        return result;
    }


    /**
     * 商家中心-验证手机号
     * @param map
     * @return
     */
    @PostMapping("/business/checkPhone")
    public ResponseResult checkPhoneByBusiness(@RequestBody Map<String, Object> map){
        ResponseResult result = userInfoService.checkPhoneByBusiness(map);
        return result;
    }


    /**
     * 平台-校验该手机号是否注册
     * @param map
     * @return
     */
    @PostMapping("/platform/checkPhone")
    public ResponseResult checkPhoneByPlatform(@RequestBody Map<String, Object> map){
        ResponseResult result = userInfoService.checkPhoneByPlatform(map);
        return result;
    }

    /**
     * 平台 查询待审核企业用户信息
     *
     * @param
     * @return
     */
    @PostMapping("/platform/businessUserList")
    public ResponseResult queryAuditList(@RequestBody Map<String, Object> param) {
        ResponseResult result = userInfoService.queryAuditList(param);
        return result;
    }

    /**
     * 企业用户信息审核
     *
     * @param
     * @return
     */
    @PostMapping("/platform/updateBusinessUser")
    public ResponseResult auditPass(@RequestBody Map<String, Object> param) {
        ResponseResult result = userInfoService.auditPass(param);
        return result;
    }

    /**
     * 用户数量
     *
     * @param map
     * @return
     */
    @PostMapping("/userCount")
    public ResponseResult userCount(@RequestBody Map<String, Object> map) {
        ResponseResult result = userInfoService.userCount(map);
        return result;
    }

    /**
     * 用户当日数量
     *
     * @param map
     * @return
     */
    @PostMapping("/userTodayNumber")
    public ResponseResult userTodayNumber(@RequestBody Map<String, Object> map) {
        ResponseResult result =userInfoService.userTodayNumber(map);
        return result;
    }


    /**
     * 修改会员登录密码
     */
    @PostMapping("/updateMemberLoginPassword")
    public ResponseResult updateMemberLoginPassword(@RequestBody Map<String,Object> param) {
        ResponseResult result=userInfoService.updateMemberLoginPassword(param);
        return result;
    }

    /**
     * 更新用户信息UserInfo单表
     */
    @PostMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody Map<String, Object> paraMap){
        ResponseResult result = userInfoService.updateUser(paraMap);
        return result;
    }

    /**
     * 查询会员
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/queryUserTurePort")
    public ResponseResult queryUserTurePort(@RequestBody Map<String, Object> paraMap) {
        ResponseResult result = userInfoService.queryUserTurePort(paraMap);
        return result;
    }

    /**
     * 修改社区用户信息
     *
     * @param param Map<Object,Object>
     * @return ResultResp
     */
    @PostMapping("/update/articleUserInfo")
    public ResponseResult updateArticleUserInfo(@RequestBody Map<Object,Object>param) {
        ResponseResult result = userInfoService.updateArticleUserInfo(param);
        return result;
    }


    /**
     * 查询用户下家列表用户列表
     *
     * @param param
     * @return
     */
    @PostMapping("/queryShareUserListPort")
    public ResponseResult queryShareUserListPort(@RequestBody Map<String, Object> param) {
        ResponseResult result = userInfoService.queryShareUserListPort(param);
        return result;
    }


    /**
     * 修改会员信息详情
     * @param param
     * @return
     */
    @PostMapping("/updateMemberMessageDetailPort")
    public ResponseResult updateMemberMessageDetailPort(@RequestBody Map<String, Object> param) {
        ResponseResult result = userInfoService.updateMemberMessageDetailPort(param);
        return result;
    }

    /**
     * 平台用户 编辑个人信息
     *
     * @param param
     * @return
     */
    @PostMapping("/limit/updatePersonalBase")
    public ResponseResult updatePersonalBase(@RequestBody UserPersonalBaseVO param) {
        ResponseResult result = userInfoService.updatePersonalBase(param);
        return result;
    }
    /**
     * 查询平台用户或者品牌店铺用户信息
     * @param checkPhoneMap
     * @return
     */
    @PostMapping("/getUserInfoByPhoneNum")
    public UserInfoVO getUserInfoByPhoneNum(@RequestBody Map<String, String> checkPhoneMap){
        UserInfoVO result =userInfoService.getUserInfoByPhoneNum(checkPhoneMap);
        return result;
    }


    /**
     * 小程序注册成功 头像 昵称 覆盖pc
     * @param updateUserParam
     */
    @PostMapping("/updateUserNickNameavAtarUrl")
    public ResponseResult updateUserNickNameavAtarUrl(@RequestBody Map<String, Object> updateUserParam){
        ResponseResult result = userInfoService.updateUserNickNameavAtarUrl(updateUserParam);
        return result;
    }

    /**
     * 第三方 授权注册  微信小程序 app 授权注册
     *
     * @param map
     * @return
     */
    @GlobalTransactional
    @PostMapping("/thirDregister")
    public ResponseResult thirDregister(@RequestBody Map<String, String> map) throws IOException, TransactionException {
        ResponseResult result = userInfoService.thirDregister(map);
        return result;
    }
    /**
     * 根据userId更改是否商家状态
     *
     * @param userId
     * @param shopId
     * @param isShop
     * @return
     */
    @GetMapping("/updateUserIsShop")
    public ResponseResult updateUserIsShop(@RequestParam("userId") String userId, @RequestParam("shopId") Integer shopId,
                                           @RequestParam("isShop") Integer isShop) {
        ResponseResult result = userInfoService.updateUserIsShop(userId, shopId, isShop);
        return result;
    }


    /**
     * 商户注册
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/addseller")
    @SysLog("商户注册")
    public ResponseResult addSeller(@RequestBody Map<String, Object> paraMap) {
        ResponseResult result = userInfoService.addSeller(paraMap);
        return result;
    }

    /**
     * 平台查询 活跃用户列表
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/platform/queryUserTrueList")
    public ResponseResult queryUserTrueListPort(@RequestBody  Map<String, Object> paraMap) {
        ResponseResult result = userInfoService.queryUserTrueListPort(paraMap);
        return result;
    }

    /**
     * 平台  查看 个人资料
     *
     * @param dto
     * @return
     */
    @PostMapping("/platform/queryMemberDetail")
    public ResponseResult queryMemberMessageDetailPort(@RequestBody MemberMessageListIntegralDTO dto) {
        ResponseResult result = userInfoService.queryMemberMessageDetailPort(dto);
        return result;
    }

    /**
     * 平台  查看 个人资料
     *
     * @param dto
     * @return
     */
    @PostMapping("/platform/queryMemberEncoded")
    public ResponseResult queryMemberMessageDetailPortEncoded(@RequestBody MemberMessageListIntegralDTO dto) {
        ResponseResult result = userInfoService.queryMemberMessageDetailPortEncoded(dto);
        return result;
    }

    /**
     * 平台修改 用户积分
     *
     * @param dto
     * @return
     */
    @PostMapping("/platform/updateMembeIntegral")
    public ResponseResult updateMemberMessageIntegral(@RequestBody MemberMessageListIntegralDTO dto) {
        ResponseResult result = userInfoService.updateMemberMessageIntegral(dto);
        return result;
    }

    /**
     * 平台 查询会员列表 导出
     *
     * @param dto
     * @return
     */
    @PostMapping("/platform/queryMemberExcel")
    public ResponseResult queryMemberMessageListPortExcel(@RequestBody MemberMessageListDTO dto) {
        ResponseResult result = userInfoService.queryMemberMessageListPortExcel(dto);
        return result;
    }


    /**
     * 平台 查询会员列表
     *
     * @param dto
     * @return
     */
    @PostMapping("/platform/queryMemberMessageList")
    public ResponseResult queryMemberMessageListPort(@RequestBody MemberMessageListDTO dto) {
        ResponseResult result = userInfoService.queryMemberMessageListPort(dto);
        return result;
    }


    /**
     * 门店注册接口
     * @param gateAddStoreDTO
     * @return
     */
    @PostMapping("/addGateStore")
    public ResponseResult addGateStore(@RequestBody(required = true) GateStoreUpdateDTO gateAddStoreDTO){
        ResponseResult result=userInfoService.addGateStore(gateAddStoreDTO);
        return result;
    }

    /**
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/register/createqr")
    public ResponseResult registerCreateqr(@RequestBody Map<String, String> paraMap) throws IOException, NotFoundException, WriterException {
        ResponseResult result=userInfoService.registerCreateqr(paraMap);
        return result;

    }


    /**
     * 品牌店铺查询会员
     * @param param
     * @return
     */
    @PostMapping("/business/queryMember")
    public ResponseResult queryMember(@RequestBody Map<String,Object> param) {
        ResponseResult result=userInfoService.queryMember(param);
        return result;
    }

    /**
     * 会员信息导出
     * @param param
     * @return
     */
    @PostMapping("/memberMessageListExp")
    public ResponseResult memberMessageListExp(@RequestBody Map<String,Object> param){
        ResponseResult result=userInfoService.memberMessageListExp(param);
        return result;
    }

    /**
     * 品牌店铺更改会员状态
     * @param param
     * @return
     */
    @PostMapping("/updateMemberStatus")
    public ResponseResult updatememberstatus(@RequestBody Map<String,Object> param) {
        ResponseResult result=userInfoService.updatememberstatus(param);
        return result;
    }

    /**
     * 校验手机号是否审核通过
     * @param paraMap
     * @return
     */
    @PostMapping("/checkPhoneReview")
    public ResponseResult checkphone(@RequestBody Map<String, String> paraMap){
        ResponseResult result=userInfoService.checkphone(paraMap.get("userMobile"));
        return result;
    }
    /**
     * 更新最后登录时间 登录次数
     * @param userId
     */
    @PostMapping("/updateLoginTimeCount")
    public ResponseResult updateLoginTimeCount(@RequestParam("userId")String userId){
        ResponseResult result=userInfoService.updateLoginTimeCount(userId);
        return result;
    }

    /**
     * 更新生日和性别
     * @param paraMap
     * @return
     */
    @PostMapping("/h5/updateUserBirthAndGender")
    public ResponseResult updateUserBirthAndGender(@RequestBody Map<String, String> paraMap){
        ResponseResult result=userInfoService.updateUserBirthAndGender(paraMap);
        return result;

    }

    /**
     * 定时任务-根据userId获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/job/queryUserInfoByjob")
    public AcquiescenceVO queryUserInfoByjob(@RequestParam("userId") String userId) {
        AcquiescenceVO result=userInfoService.queryUserInfoByjob(userId);
        return result;
    }

    /**
     * 更新用户实名认证信息
     *
     */
    @PostMapping("/verified/update")
    public ResponseResult updateVerifiedInfo(@RequestBody Map<String, Object> paraMap){
        ResponseResult result=userInfoService.updateVerifiedInfo(paraMap);
        return result;
    }
}


